package cn.iocoder.yudao.module.xkfz.service.basic.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.xkfz.domain.entity.basic.IndicatorTree;
import cn.iocoder.yudao.module.xkfz.domain.entity.rules.RulesTypeLevel;
import cn.iocoder.yudao.module.xkfz.domain.entity.rules.RulesTypeLevelGroup;
import cn.iocoder.yudao.module.xkfz.domain.po.basic.IndicatorTreePo;
import cn.iocoder.yudao.module.xkfz.mapper.basic.IndicatorTreeMapper;
import cn.iocoder.yudao.module.xkfz.mapper.rules.RulesTypeLevelMapper;
import cn.iocoder.yudao.module.xkfz.service.basic.IndicatorTreeService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class IndicatorTreeServiceImpl implements IndicatorTreeService {
    @Autowired
    IndicatorTreeMapper indicatorTreeMapper;
    @Autowired
    RulesTypeLevelMapper rulesTypeLevelMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertIndicatorTree(IndicatorTreePo indicatorTreePo) {
        //新增指标
        IndicatorTree indicatorTree1 = BeanUtil.copyProperties(indicatorTreePo, IndicatorTree.class);
        indicatorTreeMapper.insert(indicatorTree1);
        //新增规则
        return insertRulesTypeLevel(indicatorTree1,indicatorTreePo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateIndicatorTree(IndicatorTreePo indicatorTreePo) {
        //新增指标
        IndicatorTree indicatorTree1 = BeanUtil.copyProperties(indicatorTreePo, IndicatorTree.class);
        indicatorTreeMapper.updateById(indicatorTree1);
        //删除规则
        rulesTypeLevelMapper.delete("INDEX_ID", String.valueOf(indicatorTree1.getId()));
        //新增规则
        return insertRulesTypeLevel(indicatorTree1,indicatorTreePo);
    }

    private Boolean insertRulesTypeLevel(IndicatorTree indicatorTree1,IndicatorTreePo indicatorTreePo){
        //新增规则
        if ((indicatorTreePo.getRulesTypeLevels()!=null&&indicatorTreePo.getRulesTypeLevels().size()!=0)&&(
                indicatorTreePo.getRulesTypeLevels()!=null&&
                        !"".equals(indicatorTreePo.getRulesTypeLevels().get(0).getTypeLevel())
                ) ) {
            List<RulesTypeLevel> rulesTypeLevels = indicatorTreePo.getRulesTypeLevels();
            for (RulesTypeLevel rulesTypeLevel : rulesTypeLevels) {
                rulesTypeLevel.setIndexId(Long.valueOf(indicatorTree1.getId()));
                rulesTypeLevel.setPeriodTime(indicatorTreePo.getYear());
            }
            rulesTypeLevelMapper.insertBatch(rulesTypeLevels);
        }

        if(indicatorTreePo.getGroupList()!=null){
            for (RulesTypeLevelGroup rulesTypeLevelGroup : indicatorTreePo.getGroupList()) {
                List<String> idList = new ArrayList<>();
                for (RulesTypeLevel rulesTypeLevel : rulesTypeLevelGroup.getList()) {
                    rulesTypeLevel.setIndexId(Long.valueOf(indicatorTree1.getId()));
                    rulesTypeLevel.setPeriodTime(indicatorTreePo.getYear());
                    rulesTypeLevelMapper.insert(rulesTypeLevel);
                    idList.add(String.valueOf(rulesTypeLevel.getId()));
                }
                for (RulesTypeLevel rulesTypeLevel : rulesTypeLevelGroup.getList()) {
                    rulesTypeLevel.setGroupIds(idList.stream().collect(Collectors.joining(",")));
                    rulesTypeLevelMapper.updateById(rulesTypeLevel);
                }
            }
        }
        return true;
    }
    @Override
    public List<IndicatorTree> queryIndicatorTree(IndicatorTreePo indicatorTreePo) {
        List<IndicatorTree> indicatorTreeList = indicatorTreeMapper.selectList(
                new LambdaQueryWrapperX<IndicatorTree>()
                        .likeIfPresent(IndicatorTree::getIndicatorName,indicatorTreePo.getIndicatorName())
                        .eqIfPresent(IndicatorTree::getYear,indicatorTreePo.getYear())
        );
        //查询规则
        for (IndicatorTree indicatorTree : indicatorTreeList) {
            List<RulesTypeLevel> rulesTypeLevelList = rulesTypeLevelMapper.selectList("INDEX_ID", indicatorTree.getId());

            if(CollectionUtil.isNotEmpty(rulesTypeLevelList)){
                indicatorTree.setRulesTypeLevels( rulesTypeLevelList.stream()
                        .filter(rulesTypeLevel -> rulesTypeLevel.getGroupIds() == null).collect(Collectors.toList()));
            }

            List<RulesTypeLevel> rulesTypeLevels = rulesTypeLevelList.stream()
                    .filter(rulesTypeLevel -> rulesTypeLevel.getGroupIds() != null).collect(Collectors.toList());
            Map<String, List<RulesTypeLevel>> groupedByGroup = rulesTypeLevels.stream()
                    .collect(Collectors.groupingBy(RulesTypeLevel::getGroupIds));

            List<RulesTypeLevelGroup> groupList = new ArrayList<>();
            if(CollectionUtil.isNotEmpty(groupedByGroup)){
                groupList = groupedByGroup.entrySet().stream().map(entry -> {
                    RulesTypeLevelGroup rulesTypeLevelGroup = new RulesTypeLevelGroup();
                    rulesTypeLevelGroup.setGroupIds(entry.getKey());
                    rulesTypeLevelGroup.setIds(String.valueOf(entry.getValue().get(0).getGroupScore()));
                    rulesTypeLevelGroup.setList(entry.getValue());
                    return rulesTypeLevelGroup;
                }).collect(Collectors.toList());
            }else{
                groupList = new ArrayList<>();
            }
            indicatorTree.setGroupList(groupList);
        }
        return indicatorTreeList;
    }

    @Override
    public Boolean deleteIndicatorTree(String id) {
        indicatorTreeMapper.deleteById(id);
        return rulesTypeLevelMapper.delete("INDEX_ID",id)>0;
    }
}
